<template>
  <div class="app-container">
    <el-form :inline="true" :model="listQuery" size="small" label-width="100px">
      <el-form-item label="优惠券名称：">
        <el-input v-model="listQuery.name" class="input-width" placeholder="优惠券名称"></el-input>
      </el-form-item>
      <el-form-item label="优惠券类型：">
        <el-select v-model="listQuery.type" placeholder="全部" clearable class="input-width">
          <el-option v-for="item in COUPON_TYPE_MAP" :key="item.value" :label="item.label" :value="item.value"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearchList" size="small"> 搜索 </el-button>
        <el-button @click="handleResetSearch" size="small"> 重置 </el-button>
        <el-button size="small" class="btn-add" @click="handleAdd">添加</el-button>
      </el-form-item>
    </el-form>
    <div class="table-container">
      <el-table
        ref="couponTable"
        height="100%"
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="80" align="center">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="优惠劵名称" align="center" min-width="200">
          <template #default="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="优惠券类型" width="100" align="center">
          <template #default="scope">{{ COUPON_TYPE_DICT[scope.row.type] }}</template>
        </el-table-column>
        <el-table-column label="可使用商品" width="100" align="center">
          <template #default="scope">{{ COUPON_AREA_DICT[scope.row.useType] }}</template>
        </el-table-column>
        <el-table-column label="使用门槛" width="140" align="center">
          <template #default="scope">满{{ scope.row.minPoint }}元可用</template>
        </el-table-column>
        <el-table-column label="面值(元)" width="100" align="center">
          <template #default="scope">{{ scope.row.amount }}</template>
        </el-table-column>
        <el-table-column label="适用平台" width="100" align="center">
          <template #default="scope">{{ COUPON_RANGE_DICT[scope.row.platform] }}</template>
        </el-table-column>
        <el-table-column label="有效期" width="190" align="center">
          <template #default="scope">{{ formatDate(scope.row.startTime) }}~{{ formatDate(scope.row.endTime) }} </template>
        </el-table-column>
        <el-table-column label="状态" width="100" align="center">
          <template #default="scope">{{ formatStatus(scope.row.endTime) }}</template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button type="primary" size="small" link @click="handleView(scope.row)">查看</el-button>
            <el-button type="primary" size="small" link @click="handleUpdate(scope.row)"> 编辑 </el-button>
            <el-button type="danger" size="small" link @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        v-model:current-page="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script setup lang="ts">
import { fetchList, deleteCoupon } from '@/api/coupon'
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import type { Coupon, CouponPageDto } from '@/models/coupon'
import { formatDate } from '@/utils/format'
import { COUPON_TYPE_MAP, COUPON_RANGE_DICT, COUPON_TYPE_DICT, COUPON_AREA_DICT } from '@/constant'

const router = useRouter()

defineOptions({ name: 'CouponList' })
const listQuery = ref<CouponPageDto>({ pageNum: 1, pageSize: 10 })
const list = ref<Coupon[]>([])
const total = ref(0)
const listLoading = ref(false)
const multipleSelection = ref<Coupon[]>([])
getList()

const formatStatus = (endTime: any) => (new Date(endTime).getTime() > Date.now() ? '未过期' : '已过期')

function handleResetSearch() {
  listQuery.value = { pageNum: 1, pageSize: 10 }
}

function handleSearchList() {
  listQuery.value.pageNum = 1
  getList()
}

function handleSelectionChange(val: Coupon[]) {
  multipleSelection.value = val
}

function handleSizeChange(val: number) {
  listQuery.value.pageNum = 1
  listQuery.value.pageSize = val
  getList()
}

function handleCurrentChange(val: number) {
  listQuery.value.pageNum = val
  getList()
}

function handleAdd() {
  router.push({ path: '/sms/couponUpsert' })
}

function handleView(row: Coupon) {
  router.push({ path: '/sms/couponDetail', query: { id: row.id } })
}

function handleUpdate(row: Coupon) {
  router.push({ path: '/sms/couponUpsert', query: { id: row.id } })
}

async function handleDelete(row: Coupon) {
  await ElMessageBox.confirm('是否进行删除操作?', '提示', { type: 'warning' })
  await deleteCoupon(row.id)
  ElMessage.success('删除成功')
  getList()
}

async function getList() {
  listLoading.value = true
  const response = await fetchList(listQuery.value)
  listLoading.value = false
  list.value = response.data.data.list
  total.value = response.data.data.total
}
</script>
<style scoped>
.input-width {
  width: 203px;
}
</style>
